//
//  WordsViewController.m
//  OurRelaxDemo
//
//  Created by 于宾尧 on 15/7/3.
//  Copyright (c) 2015年 于宾尧. All rights reserved.
//

#import "WordsViewController.h"

@interface WordsViewController ()

@end

@implementation WordsViewController

-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        [self subMyViews];
        self.myWords.myDelegate = self;

    }
    return self;
}

-(void)subMyViews
{
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    
    
    flowLayout.itemSize = CGSizeMake(self.view.frame.size.width, self.view.frame.size.height);
    
    
    flowLayout.minimumLineSpacing = 0;
    
    
    
    
    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    
    
    
    UICollectionView *myCollectionV = [[UICollectionView alloc]initWithFrame:CGRectMake(-5, 0, self.view.frame.size.width, self.view.frame.size.height)  collectionViewLayout:flowLayout];
    
    
    myCollectionV.pagingEnabled = YES;
    
    [self.view addSubview:myCollectionV];
    myCollectionV.delegate = self;
    myCollectionV.dataSource = self;
    
    
    [myCollectionV registerClass:[wordsCollectionViewCell class] forCellWithReuseIdentifier:@"reuse"];
    
//    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(backAction)];
    
    
}

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 200;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    self.myWords = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
    NSLog(@"%ld",indexPath.row);
    NSDate *sendDate = [NSDate date];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    NSString *urlDate = [dateFormatter stringFromDate:sendDate];
    NSDate * date = [dateFormatter dateFromString:urlDate];
    NSDate * newDate = [NSDate dateWithTimeInterval:indexPath.row *(- 24 * 60 * 60) sinceDate:date];
    NSString *newStr = [dateFormatter stringFromDate:newDate];
    [self getWordsData:newStr];

    
    
    return self.myWords;
    
}

-(void)backToRoot
{
    
}

-(void)getWordsData:(NSString *)dateStr
{
    AFNetworkReachabilityManager *netWorkManager = [AFNetworkReachabilityManager sharedManager];
    
    
    NSString *url_string = [NSString stringWithFormat:@"http://bea.wufazhuce.com/OneForWeb/one/getHp_N?strDate=%@&strRow=1",dateStr];
    
    
    //[NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html",nil];  代表支持所有的接口类型
    
    AFHTTPRequestOperationManager* manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html",nil];
    [manager GET:url_string parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        [netWorkManager stopMonitoring];
        NSLog(@"使用AFN进行get请求 ===  %@",responseObject);
        
        NSMutableDictionary *dic = responseObject;
        NSMutableDictionary *dataDic = [dic objectForKey:@"hpEntity"];
        self.wordMdl = [[wordModel alloc]init];
        [self.wordMdl setValuesForKeysWithDictionary:dataDic];
        
        self.myWords.wordMdl = self.wordMdl;


        
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        NSLog(@"失败==== %@",error);
        
    }];

    
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    
}


//-(void)backAction
//{
//    [self dismissViewControllerAnimated:YES completion:^{
//        
//        
//    }];
//}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
